<?php
class Request_model extends CI_Model{
	function __construct(){
		parent::__construct();
	}

	public function add($item_barcode, $branch_id, $request_amount){
		
		$this->load->model('item_model');
		$item_id = $this->item_model->get_item_by_barcode($item_barcode)->item_id;
		$data = array(
				'item_id' => $item_id,
				'branch_id' => $branch_id,
				'request_amount' => $request_amount
		);
		if ($this->db->insert('request', $data))
			return $this->db->insert_id();
		else
			return -1;
	}

	public function approved($request_id){
		$this->db->set('request_approved_date','NOW()', false);
		$this->db->where('request_id', $request_id);
		$this->db->where('item_active', 1);
		$this->db->where('request_approved_date is ', 'null', false);
		if ($this->db->update('request join item on item.item_id = request.item_id'))
			return $this->db->affected_rows() ? true : false;
		return false;
	}

	public function received($request_id, $branch_id){
		$this->db->set('request_received_date','NOW()', false);
		$this->db->where('request_id', $request_id);
		$this->db->where('branch_id', $branch_id);
		$this->db->where('request_approved_date is not ', 'null', false);
		$this->db->where('request_received_date is ', 'null', false);
		if ($this->db->update('request'))
			return $this->db->affected_rows() ? true : false;
		return false;
	}

	public function get_id($request_name){
		//$query = $this->db->get_where('request', array('request_name' => $request_name));
		$query = $this->db->where('LOWER(request_name)', strtolower(utf8_encode($request_name)))->get('request');

		if ($query->num_rows() > 0)
		{
			$row = $query->row();
			return $row->request_id;
		}else
			return -1;
	}

	public function get_request_by_id($id){
		$this->db->select('*');
		$this->db->from('request');
		$this->db->join('item', 'request.item_id = item.item_id');
		$this->db->where('request_id', $id);
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->row();
		}else
			return null;
	}

	public function get_new_request(){
		$this->db->select('*');
		$this->db->from('request');
		$this->db->join('item', 'request.item_id = item.item_id');
		$this->db->join('inventory', 'inventory.item_id = item.item_id');
		$this->db->where('request_approved_date is ', 'null', false);
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			$requests = $query->result();
			foreach ($requests as $request){
				if($request->item_active == 0)
					$request->status = Constants::item_status_inactive;
				else if ($request->inventory_current_stock >= $request->request_amount)
					$request->status = Constants::item_status_normal;
				else
					$request->status = Constants::item_status_no_stock;
			}
			return $requests;
		}else
			return null;
	}

	public function get_item_not_received_request(){
		$this->db->select('*');
		$this->db->from('request');
		$this->db->join('item', 'request.item_id = item.item_id');
		$this->db->join('branch', 'branch.branch_id = request.branch_id');
		$this->db->where('request_approved_date is ', 'not null', false);
		$this->db->where('request_received_date is ', 'null', false);
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}else
			return null;
	}
	
	public function get_request_count(){
		$this->db->from('request');
		$this->db->where('request_approved_date is ', 'null', false);
		return $this->db->count_all_results();
	}

	public function search($string){
		if (strlen($string)>0){
			$this->db->like('request_name', $string, 'after');
			$this->db->or_like('request_name', ' '.$string);
			$query = $this->db->get('request');
			if ($query->num_rows() > 0)
			{
				return $query->result();
			}
		}
		return null;
	}
}